package Nav_Bottom;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ScrollView;

import com.example.anxiang.R;
import com.example.anxiang.activity.api.Api;
import com.example.anxiang.activity.api.ApiConfig;
import com.example.anxiang.activity.api.MsgCallback;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.zhpan.bannerview.BannerViewPager;

import java.util.ArrayList;
import java.util.List;

import Banner.BannerAdaper;
import Banner.BannerData;

/**
 * The type Home fragment.
 */
public class HomeFragment extends Fragment {
    private View view;
    private Button Bttest;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_home, container, false);
        }
        Bttest=view.findViewById(R.id.test);
        Bttest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                test();
            }
        });
        LineChart();//折线图
        new Banner(view);
        return view;
    }
    private void test() {
        Api.config(ApiConfig.TEST).getRequest(new MsgCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }
    private void LineChart(){
        LineChart lineChart = view.findViewById(R.id.linechart);
        List<Entry> entries = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            entries.add(new Entry(i, (float) (Math.random()) * 80));
        }
        LineDataSet lineDataSet = new LineDataSet(entries, "温度");
        LineData data = new LineData(lineDataSet);
        lineChart.setData(data);
    }
}

/**
 * The type Banner.
 */
//    轮播图
class Banner extends Fragment{
    /**
     * Instantiates a new Banner.
     *
     * @param view the view
     */
    public Banner(View view){
        BannerViewPager<BannerData> bannerViewPager = view.findViewById(R.id.banner_view);
        bannerViewPager.setAdapter(new BannerAdaper())
                .setLifecycleRegistry(getLifecycle())
                .setAutoPlay(true)
                .setRoundCorner(20)
                .create(getBannerData());
    }

    /**
     * Gets banner data.
     *
     * @return the banner data
     */
    public static List<BannerData> getBannerData() {
        List<BannerData> list = new ArrayList<>();
        BannerData bannerData1 = new BannerData();
        bannerData1.setImageUrl("https://ss1.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/a044ad345982b2b75133e15130adcbef77099bf3.jpg");
        BannerData bannerData2 = new BannerData();
        bannerData2.setImageUrl("https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/d009b3de9c82d1582442d267820a19d8bd3e4220.jpg");
        BannerData bannerData3 = new BannerData();
        bannerData3.setImageUrl("https://ss3.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/0824ab18972bd407ff38f2b378899e510eb30984.jpg");
        list.add(bannerData1);
        list.add(bannerData2);
        list.add(bannerData3);
        return list;
    }
}